package introse;

import java.util.List;

public class clientViewer extends javax.swing.JPanel {

    private clientManager clientMngr;
    private uneditableTableModel clientTableModel;

    public clientViewer() {
        initComponents();
    }

    //Load clients from database to table
    public void loadClientTable() {
        //Reset Client List And Table
        clientMngr = new clientManager();
        clientTableModel = new uneditableTableModel(new String[]{"Client Number", "Is Active", "Name", "Address", "Contact Number"});
        clientTable.setModel(clientTableModel);

        //Hide first 2 columns (Client number and isActive
        clientTable.removeColumn(clientTable.getColumnModel().getColumn(0));
        clientTable.removeColumn(clientTable.getColumnModel().getColumn(0));

        //Load clients from clientManager to table
        for (client clnt : clientMngr.getClients(true)) {
            clientTableModel.addRow(new String[]{Integer.toString(clnt.getClientNumber()), Boolean.toString(clnt.getIsActive()), clnt.getClientName(), clnt.getClientAddress(), Integer.toString(clnt.getContactNumber())});
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addClient = new javax.swing.JButton();
        deactivateClient = new javax.swing.JButton();
        editClient = new javax.swing.JButton();
        tableScrollPanel = new javax.swing.JScrollPane();
        clientTable = new javax.swing.JTable();
        reactivateClients = new javax.swing.JButton();

        setPreferredSize(new java.awt.Dimension(1000, 600));

        addClient.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Client Add.png"))); // NOI18N
        addClient.setText("Add Client");
        addClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        addClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        addClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addClientActionPerformed(evt);
            }
        });

        deactivateClient.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Client Minus.png"))); // NOI18N
        deactivateClient.setText("Deactivate Client");
        deactivateClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        deactivateClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        deactivateClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deactivateClientActionPerformed(evt);
            }
        });

        editClient.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Client Edit.png"))); // NOI18N
        editClient.setText("Edit Client");
        editClient.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editClient.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editClientActionPerformed(evt);
            }
        });

        clientTable.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        clientTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        clientTable.setEditingRow(0);
        clientTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        tableScrollPanel.setViewportView(clientTable);

        reactivateClients.setIcon(new javax.swing.ImageIcon(getClass().getResource("/introse/images/Revert Client.png"))); // NOI18N
        reactivateClients.setText("Reactivate Clients");
        reactivateClients.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        reactivateClients.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        reactivateClients.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reactivateClientsActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tableScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 855, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(deactivateClient, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(editClient, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(addClient, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(reactivateClients))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(addClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deactivateClient)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(reactivateClients)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(tableScrollPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 578, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addClientActionPerformed
        new addClient(null, true).setVisible(true);
        loadClientTable();
    }//GEN-LAST:event_addClientActionPerformed

    private void editClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editClientActionPerformed
        try {
            int clientNumber = Integer.parseInt(clientTableModel.getValueAt(clientTable.getSelectedRow(), 0).toString());
            if (clientNumber != 1) {
                editClient editClnt = new editClient(null, true);
                editClnt.setClient(clientMngr.getClient(Integer.parseInt(clientTableModel.getValueAt(clientTable.getSelectedRow(), 0).toString())));
                editClnt.setVisible(true);
            } else {
                errorDialogue errorMessage = new errorDialogue(null, true);
                errorMessage.setMessage("Walk-in client cannot be edited");
                errorMessage.setVisible(true);
            }

        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No client selected");
            errorMessage.setVisible(true);
        }
        loadClientTable();
    }//GEN-LAST:event_editClientActionPerformed

    private void deactivateClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deactivateClientActionPerformed
        try {
            int clientNumber = Integer.parseInt(clientTableModel.getValueAt(clientTable.getSelectedRow(), 0).toString());
            if (clientNumber != 1) {
                deactivationDialog delDiag = new deactivationDialog(null, true);
                delDiag.setVisible(true);
                if (delDiag.getReturnStatus() == 1) {
                    clientMngr.getClient(clientNumber).setIsActive(false);
                    positiveMessage posMsg = new positiveMessage(null, true);
                    posMsg.setMessage("Client has been deactivated");
                    posMsg.setVisible(true);
                }
            } else {
                errorDialogue errorMessage = new errorDialogue(null, true);
                errorMessage.setMessage("Walk-in client cannot be deactivated");
                errorMessage.setVisible(true);
            }

        } catch (Exception ex) {
            errorDialogue errorMessage = new errorDialogue(null, true);
            errorMessage.setMessage("No client selected");
            errorMessage.setVisible(true);
        }
        loadClientTable();
    }//GEN-LAST:event_deactivateClientActionPerformed

    private void reactivateClientsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reactivateClientsActionPerformed
        reactivateClients reactClnts = new reactivateClients(null, true);
        reactClnts.setVisible(true);
        loadClientTable();
    }//GEN-LAST:event_reactivateClientsActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addClient;
    private javax.swing.JTable clientTable;
    private javax.swing.JButton deactivateClient;
    private javax.swing.JButton editClient;
    private javax.swing.JButton reactivateClients;
    private javax.swing.JScrollPane tableScrollPanel;
    // End of variables declaration//GEN-END:variables
}
